package com.mango.leet.code.week1.game390;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @Author: mango
 * @Date: 2024/3/24 10:51
 */
public class LC100245 {

    public int maximumLengthSubstring(String s) {
        int result = 0;
        int size = s.length();
        for (int i = 0; i < size; i++) {
            for (int j = i+1; j <= size-1; j++) {
                String subStr = s.substring(i, j+1);
                if (countGt(subStr, 2)) {
                    break;
                }
                result = Math.max(result, subStr.length());
            }
        }
        return result;
    }

    private boolean countGt(String subStr, int count) {
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < subStr.length(); i++) {
            Integer theCount = map.getOrDefault(subStr.charAt(i), 0) + 1;
            if (theCount > count) {
                return true;
            }
            map.put(subStr.charAt(i), theCount);
        }
        return false;
    }

    public static void main(String[] args) {
        //String s = "bcbbbcba";
        //String s = "aaaa";
        String s = "eebadadbfa";
        LC100245 r = new LC100245();
        System.out.println(r.maximumLengthSubstring(s));
    }

}
